package com.nineclock.attendance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nineclock.attendance.dto.MakeUpDTO;
import com.nineclock.attendance.dto.MakeupDtoT;
import com.nineclock.attendance.mapper.MakeUpMapper;
import com.nineclock.attendance.pojo.MakeUp;
import com.nineclock.attendance.service.MakeUpService;
import com.nineclock.common.entity.PageResult;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.filter.CurrentUserHolder;
import com.nineclock.common.utils.BeanHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class MakeUpServiceImpl implements MakeUpService {

    @Autowired
    private MakeUpMapper makeUpMapper;

    /*
    * 分页查询补卡规则
    * */
    @Override
    public PageResult<MakeUpDTO> makeupPage(Integer page, Integer pageSize) {
        Page<MakeUp> ipage = new Page<>(page, pageSize);
        LambdaQueryWrapper<MakeUp> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MakeUp::getCompanyId, CurrentUserHolder.get().getCompanyId());
        ipage = makeUpMapper.selectPage(ipage, wrapper);

        //获取当前页数据
        List<MakeUp> records = ipage.getRecords();
        if(!CollectionUtils.isEmpty(records)) {
            List<MakeUpDTO> makeUpDTOS = BeanHelper.copyWithCollection(records, MakeUpDTO.class);
            return new PageResult<>(ipage.getTotal(), ipage.getPages(), makeUpDTOS);
        }
        return null;
    }


    /*
    * 修改补卡规则
    * */
    @Override
    public void makeupPut(MakeUpDTO makeUpDTO) {
        if(makeUpDTO==null){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        MakeUp makeUp = BeanHelper.copyProperties(makeUpDTO, MakeUp.class);
        makeUpMapper.updateById(makeUp);
    }
}
